// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Released under the terms of the GNU General Public License version 2 or later.

package eg.music
{
import mx.formatters.DateFormatter;
import com.fit4as.fit.Exception;
import mx.formatters.DateBase;



public class Music {

    public static var status : String = "ready";

    public var title : String;
    public var artist : String;
    public var album : String;
    public var genre : String;
    public var size : Number;
    public var seconds : int;
    public var trackNumber : int;
    public var trackCount : int;
    public var year : int;
    public var date : MusicDate;
    public var selected : Boolean = false;


    // Accessors ////////////////////////////////

    public function track() : String {
        return trackNumber + " of " + trackCount;
    }

    public function time() : Number {
        return Math.round(seconds / 0.6) / 100.0;
    }

    public function toString() : String {
        if (title != null) {
            return title;
        } else {
            return super.toString();
        }
    }


    // Factory //////////////////////////////////

    public static function parse(string : String) : Music {
        //t race("Music#parse " + string );
        var m : Music = new Music();
        var t : Array = string.split("\t");
        m.title = t.shift();
        m.artist = t.shift();
        m.album = t.shift();
        m.genre = t.shift();
        m.size = parseLong(t.shift());
        m.seconds = parseInt(t.shift());
        m.trackNumber = parseInt(t.shift());
        m.trackCount = parseInt(t.shift());
        m.year = parseInt(t.shift());
        m.date = parseDate(t.shift());
        return m;
    }

    public static function parseDate(s : String) : MusicDate
    {
        return MusicDate.parse(s);
    }

    private static function parseLong(s : String) : Number
    {
        var result : Number = parseFloat(s);
        if (result % 1 > 0) throw new Exception("NumberFormatException: For input string: \"" + s + "\"");
        return result;
    }

}
}